﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Phone.Controls;
using wordroid.Model;
using wordroid.Resources;
using wordroid.utils;

namespace wordroid.View {
    public partial class EditWordPage : PhoneApplicationPage {
        
        public EditWordPage() {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) {
            base.OnNavigatedTo(e);
            string word_id = String.Empty;

            if (NavigationContext.QueryString.TryGetValue("word_id", out word_id)) {
                
                int id = int.Parse(word_id);
                Word word = DBHelper.getInstance().getWordById(id);
                
                if(word != null) {
                    fillForm(word);
                }

                _current_word_id = id;
            } else {
                //this is the "create word" page, but not "Edit word"
                PageTitle.Text = AppResources.TitleCreateWord;
            }
        }

        private void createTranslationsPanel(bool isValid) {
            createTranslationsPanel(isValid, String.Empty);
        }

        private void createTranslationsPanel(bool isValid, string text) {
            Panel panel = isValid ? TranslationsPanel : InvTranslationsPanel;
            ButtonedEditField field = new ButtonedEditField() { Text = text };
            field.RemoveClick = new RoutedEventHandler((sender, args) => { panel.Children.Remove(field);});

            panel.Children.Add(field);
        }

        private void fillForm(Word word) {
            OriginalWordTextBox.Text = word.originalWord;
            
            List<Translation> trans = DBHelper.getInstance().getTranslations(word);
            foreach (Translation translation in trans) {
                createTranslationsPanel(translation.isValid, translation.translation);
            }
        }

        private void AddTranslation_Click(object sender, RoutedEventArgs e) {
            createTranslationsPanel(true);
        }

        private void AddInvTranslation_Click(object sender, RoutedEventArgs e) {
            createTranslationsPanel(false);
        }

        #region shell
        
        private void SaveButton_Click(object sender, EventArgs e) {
            
                try {
                    if(_current_word_id == null) {
                        DBHelper.getInstance().SaveWordAsNew(OriginalWordTextBox.Text, 
                        getTranslationsList(true), getTranslationsList(false));
                    } else {
                        DBHelper.getInstance().EditWord(_current_word_id, OriginalWordTextBox.Text,
                                getTranslationsList(true), getTranslationsList(false));
                    }
                } catch (BaseException exception) {
                    MessageBox.Show(exception.Message);
                    return;
                }

            MessageBox.Show(AppResources.MessageWordSavedSuccessfully);
            NavigationService.GoBack();
        }

        private List<String>  getTranslationsList(bool isValid) {
            
            List<String> translations = new List<string>();

            Panel parent = isValid ? TranslationsPanel : InvTranslationsPanel;
            foreach (Control control in parent.Children) {
                if(control is ButtonedEditField) {
                    String text = (control as ButtonedEditField).Text;
                    if(!String.IsNullOrWhiteSpace(text) && !translations.Contains(text)) {
                        translations.Add(text);
                    }
                }
            }

            return translations;
        }

        private void Button_Click_Back(object sender, EventArgs eventArgs) {
            NavigationService.GoBack();
        }

        #endregion

        private int? _current_word_id;
    }
}